from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from django.contrib.auth.hashers import make_password

class Command(BaseCommand):
    help = '删除所有用户并重新创建管理员账户'

    def add_arguments(self, parser):
        parser.add_argument('--admin-password', type=str, help='管理员账户的新密码', default='admin123')

    def handle(self, *args, **options):
        # 删除所有用户
        User.objects.all().delete()
        self.stdout.write(self.style.SUCCESS('已删除所有用户'))

        # 创建新的管理员账户
        admin_password = options['admin_password']
        User.objects.create_superuser(
            username='admin',
            email='admin@example.com',
            password=admin_password
        )
        self.stdout.write(self.style.SUCCESS(f'已创建新的管理员账户 (用户名: admin, 密码: {admin_password})')) 